class ElectronCenter {
  private events: Record<string, any[]> = {};

  // 订阅事件
  on(eventName: string, callback: any) {
    if (!this.events[eventName]) {
      this.events[eventName] = [];
    }
    this.events[eventName].push(callback);
    return this;
  }

  // 取消订阅
  off(eventName: string, callback?: any) {
    if (!this.events[eventName]) return this;

    if (!callback) {
      delete this.events[eventName];
      return this;
    }

    this.events[eventName] = this.events[eventName].filter(
      (cb) => cb !== callback,
    );
    return this;
  }

  // 发布事件
  emit(eventName: string, ...args: any[]) {
    if (!this.events[eventName]) return this;

    this.events[eventName].forEach((callback) => {
      callback(...args);
    });
    return this;
  }

  // 只订阅一次
  once(eventName: string, callback: any) {
    const onceCallback = (...args: any[]) => {
      callback(...args);
      this.off(eventName, onceCallback);
    };

    return this.on(eventName, onceCallback);
  }
}

// 创建单例实例
const electronCenter = new ElectronCenter();

// 暴露到window对象
(window as any).electronCenter = electronCenter;

export default electronCenter;
